<?php
/**
 * 获取每日考勤记录详情bll
 */
apf_require_class("Bll_BLL");
class Bll_Attendance_AccessDayRecord extends BLL{

    /**
     * 根据用户ID获取每日考勤详情
     *
     * @param int $user_id
     * @param int $row = 7
     * @param int  $page = 1
     * @access public
     * @return void
     */
    public function get_record_by_user_id($user_id, $row = 7,$page = 1) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $back = $this->get_app_dao()->find_by_id($user_id,$row,$page);
        if($back) {
            $result = $back;
        }
        return $result;
    }

    /**
     * 根据用户ID及起止时间获取用户打开记录
     *
     * @param mixed $user_id
     * @param int $start
     * @param int $end
     * @access public
     * @return void
     */
    public function get_record_list($user_id,$start = 0,$end = 0) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }

        $result =$this->get_app_dao()->get_day_record($user_id,$start,$end);
        //echo '<pre>'.print_r($result, TRUE).'</pre>';
        return $result;//$result 返回  用户七天内的考勤记录
    }

    /**
     * 获取我的考勤及打卡信息
     *
     * @param mixed $user_id
     * @param int $start
     * @param int $end
     * @access public
     * @return void
     */
    public function get_my_attendance($user_id,$start = 0, $end = 0){
        $result = array();
        if(empty($user_id)){
            return $result;
        }
        apf_require_class('Bll_Attendance_LeaveRecord');
        $bll = new Bll_Attendance_LeaveRecord();
        $leave_record = $bll->get_leave_data($user_id);
        apf_require_class('Bll_Attendance_UserInfo');
        $u_bll = new Bll_Attendance_UserInfo();
        $uinfo = $u_bll->get_user_by_ids($user_id);
        $result['allleaverecord'] = $leave_record;
        $tmp = $this->get_record_list($user_id,$start,$end);//返回  用户七天内的考勤记录
        $work_weekend = Tools_Tools::get_instance()->special_work_day();
        $holiday = Tools_Tools::get_instance()->each_year_holiday_country();
        $take_holiday = Tools_Tools::get_instance()->holiday_country();
        $no_work_day = array_merge($holiday,$take_holiday);
        //处理特殊下班时间
        $special_leave_times = Tools_Tools::get_instance()->special_leave_time();

        if(empty($tmp)) {
            return $result;
        }
        foreach ($tmp as $key => $val) {
            $keep_date = $val['access_date'];
            $access_date = $val['access_date'] = strtotime($val['access_date']);

            $val['month'] = date('m',$val['access_date']);
            $val['day'] = date('d',$val['access_date']);
            $val['week'] = $this->convert_chinese_week(date('w',$val['access_date']));
            $val['start'] = ($val['start_work'])?intval(substr($val['start_work'],0,2)).":".substr($val['start_work'],2,2):0;
            $val['end'] = ($val['end_work'])?substr($val['end_work'],0,2).":".substr($val['end_work'],2,2):0;
       //   $val['worktime'] = $this->get_work_time($val['start'],$val['end']);
            $val['worktime'] = $this->get_work_time($val['start_work'],$val['end_work']);
            $val['workendtime'] = $this->get_workend_time($keep_date,$val['end']);
            if(in_array(date('Y-m-d',$access_date), $work_weekend)){
                $val['sp_work_day'] = 1;
            }
            if(in_array(date('Y-m-d',$access_date), $no_work_day)){
                continue;
            }
            if($keep_date == date('Ymd') && $val['end_work'] < 1800) {//如果是当天，并且当天还不到18点，则暂时不显示考勤打卡结束时间
                $val['end'] = 0;
            }
            $start_work = $val['start_work']= intval($val['start_work']);
            $end_work = $val['end_work'] = intval($val['end_work']);

            $status = array();
            //判断迟到状态
            if($start_work > 930) {
                $status[0] = array('id' => 3,'name' => '上午缺勤');//缺勤
            } elseif ($start_work > 910 && $start_work <= 930) {
                $status[0] = array('id' => 1,'name' => '迟到');//迟到
            }
            //判断早退状态 当天不做判断

            if($end_work <= 1729 && $keep_date != date('Ymd')) {
                if($status[0]['id'] != 3) {
                    $status[1] = array('id' => 3,'name' => '下午缺勤');//缺勤
                }
            } elseif ($end_work >= 1730 && $end_work < 1800 && $keep_date != date('Ymd')) {
                $status[1] = array('id' => 2,'name' => '早退');//早退
            }
            if($start_work >= 1800 && $end_work > 1800 && $keep_date != date('Ymd')) {
                $status[1] = array('id' => 3,'name' => '下午缺勤');//缺勤
                unset($status[1]);
            }

            if($start_work >= 1431 && $end_work > 1800 && $keep_date != date('Ymd')) {
                if($start_work <1801){
                    //1431-1800  1800+
                }else{
                    $status[1] = array('id' => 3,'name' => '下午缺勤');//缺勤
                }
            }
            if($start_work >=931 && $end_work <= 1729 && $keep_date != date('Ymd')) {
                $status[0] = array('id' => 3,'name' => '上午缺勤');//缺勤
                $status[1] = array('id' => 3,'name' => '下午缺勤');//缺勤
                #$status[1] = array('id' => 3,'name' => '全天缺勤');//缺勤
                #unset($status[0]);
            }
            if(empty($start_work) && empty($end_work) && $keep_date != date('Ymd')) {
                $status[1] = array('id' => 3,'name' => '全天缺勤');//缺勤
                unset($status[0]);
            }
            //如果为当天，19点后判断是否早退
            if($keep_date == date('Ymd') && date('H') < 23) {
                unset($status[1]);
            }
            //判断特殊班别
            if($uinfo[0]['user_type'] == 1 && date('Ymd',$access_date) >= $uinfo[0]['user_type_date']) {
                unset($status);
            }
            //结束判断特殊班别
            //周末去掉状态
            if(date('w',$access_date) == '6' || date('w',$access_date) == '0') {
                if(!in_array(date('Y-m-d',$access_date), $work_weekend)) {
                    $status[0] = array('id' => 999,'name' => '周末加班，辛苦啦！');
                    unset($status[1]);
                }
            } elseif(empty($status)) {//如无其他状态，正常状态，当天的出外
                $status[0] = array('id' => 999,'name' => '正常');
                if($keep_date == date('Ymd') && $val['end_work'] < 1800) {
                    unset($status);
                }
            }
            //检查用户是否有请假记录
            $have_holiday = false;
            if($start_work > 910 && $leave_record) {
                //$ajust = mktime(9,31,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                if ($start_work <= 1410) {
                    //如果请14:00结束的假，取931做判断，其它的只有公出抵扣
                    $ajust = mktime(9,31,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                } elseif ($start_work > 1410) {
                    $ajust = mktime(9,31,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                } else {
                    //$ajust = mktime(substr($start_work, 0,-2),substr($start_work, -2,2),0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                }
                //迟到
                $day_cd = date("Y-m-d",$ajust);
                foreach ($leave_record as $k => $v) {
                    $waitpass = false;
                    if($v['status'] == '已批准' || $v['status'] == '未审核') {
                        $waitpass = true;
                    }
                    if(($v['type'] !='缺勤' && $v['type'] != '迟到' && $v['type'] != '早退') && $ajust >= $v['start'] && $ajust <= $v['end'] && $waitpass) {
                        $status[0] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                        $reason = "类别:".$v['leave_reason'].$desc;
                        //病假，陪产假特殊处理
                        if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                            if((time() - $v['apply_time_ext']) < 604800) {
                                $reason = $v['type']."<a href='/proof/?id=".$v['id']."'>上传证明</a>";
                            }
                        }
                        $val['reason'][] = $reason;
                        //$have_holiday = true;
                    } elseif(($v['type'] == '缺勤' || $v['type'] =='迟到') && $day_cd == date("Y-m-d",$v['start']) && $waitpass){
                        $status[0] = array('id' => 999,'name' => $v['type']."(".$v['leave_reason'].")",'status'=>"({$v['status']})",'keep_type' => $v['leave_reason']);
                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                        $reason = "类别:".$v['type'].$desc;
                        $val['reason'][] = $reason;
                    }
                    if ($start_work > 910 && $end_work >=1800 && $ajust >= $v['start']) {
                        //离开时间大于1800
                        $full_day = mktime(18,00,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                        if (($v['type'] == '缺勤' || $v['type'] =='迟到') && $v['leave_reason'] == '忘记刷卡') {
                            $v['end'] += 86399;
                        }
                        if ($full_day <= $v['end'] && $ajust <= $v['end']) {
                            unset($status[1]);
                        }
                    }
                 }
                 //去掉有假时，全天缺勤情况
                 if($status[0]['id'] == 999 && $status[1]['name'] == '全天缺勤') {
                     $status[1] = array('id' => 3,'name' => '下午缺勤');//缺勤
                 }
            }
            //检查下午是否有请假情况
            if($end_work <= 1800 && $leave_record && date('Ymd',$access_date)!=date('Ymd')) {
                //$afternoon = mktime(14,30,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                $afternoon_18 = mktime(18,0,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));

                //迟到
                $day_cd = date("Y-m-d",$afternoon_18);
                $same_leave = false;
                foreach ($leave_record as $k => $v) {

                    $waitpass = false;
                    if($v['status'] == '已批准' || $v['status'] == '未审核') {
                        $waitpass = true;
                    }
                    if($v['status']=='已驳回'){
                        continue;
                    }
                    if($v['type'] == '缺勤' || $v['type'] == '迟到' || $v['type'] == '早退') {
                        $v['end'] += 86399;
                    }
                    if($v['type'] =='公出' && $v['end'] < $afternoon_18){
                        continue;
                    }
                    if($v['type'] != '公出'){
                        $afternoon = mktime(14,0,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                    }else{//将全局变量改成局部变量 20121015
                        if($end_work <= 1430) {
                            $afternoon = mktime(14,30,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                        } else {
                            $afternoon = mktime(substr($end_work, 0,-2),substr($end_work, -2,2),0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                        }
                    }
                    if(($v['type'] !='缺勤' && $v['type'] != '迟到' && $v['type'] != '早退') && $afternoon >= $v['start'] && $afternoon <= $v['end'] && $waitpass) {

                        if(!$have_holiday) {
                        $status[1] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                        $reason = "类别:".$v['leave_reason'].$desc;
                        //病假，陪产假特殊处理
                        if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                            if((time() - $v['apply_time_ext']) < 604800) {
                                $reason = $v['type']."<a href='/proof/?id={$v['id']}'>上传证明</a>";
                            }
                        }
                        $val['reason'][] = $reason;
                        }
                    } elseif($v['type'] == '缺勤' && $afternoon >= $v['start'] && $afternoon <= $v['end'] && $waitpass){
                        if ($v['leave_reason'] != '地铁故障') {
                            if ($v['leave_reason'] == '忘记带卡') {//可以抵一天
                                $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                $reason = "类别:".$v['type'].$desc;
                                $status[1] = array('id' => 999,'name' => $v['type']."(".$v['leave_reason'].")",'status'=>"({$v['status']})");
                                $val['reason'][] = $reason;
                            } elseif($v['leave_reason'] == '忘记刷卡' || $v['leave_reason'] == '其它') {
                                if($same_leave || $v['type'] == '早退' || ($status[0]['name'] != '缺勤' && $status[0]['name'] != '缺勤(其它)' && $status[0]['name'] != '缺勤(忘记刷卡)')) {
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['type'].$desc;
                                    $status[1] = array('id' => 999,'name' => $v['type']."(".$v['leave_reason'].")",'status'=>"({$v['status']})");
                                    $val['reason'][] = $reason;
                                }
                                if(isset($status[0]) && ($status[0]['keep_type'] == '其它' || $status[0]['keep_type'] == '忘记刷卡') && !$same_leave) {
                                    $same_leave = true;
                                }
                            }
                        }
                    }elseif($v['type'] == '早退' && $afternoon >= $v['start'] && $afternoon <= $v['end'] && $waitpass){//早退特殊处理
                        if ($v['leave_reason'] != '地铁故障') {
                            if ($v['leave_reason'] == '忘记带卡') {//可以抵一天
                                //unset($status[1]);
                            } elseif($v['leave_reason'] == '忘记刷卡' || $v['leave_reason'] == '其它') {
                                if($v['type'] == '早退') {
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['type'].$desc;
                                    $status[1] = array('id' => 999,'name' => $v['type']."(".$v['leave_reason'].")",'status'=>"({$v['status']})");
                                    $val['reason'][] = $reason;
                                }
                            }
                        }
                    }//endif

                    //如果请一天假，只去其中一个
                    $curr_start = mktime(9,0,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                    $curr_end = mktime(18,0,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                    if(count($status) == 2 && $curr_start >= $v['start'] && $curr_end <= $v['end']) {
                        if($status[0]['id'] == $status[1]['id'] && $status[0]['name'] == $status[1]['name']) {
                            unset($status[0]);
                        }
                    }
                }
                //上下午排序
                if(!empty($status) && is_array($status)){
                    ksort($status);
                }
            }
            //特殊下班时间处理
            $special_day = date('Y-m-d',strtotime($keep_date));
            if (isset($special_leave_times[$special_day]) && intval(date('Hi',strtotime($special_leave_times[$special_day]['leave_time']))) <= $end_work) {
                $status[1]['name']='正常';
                $val['end_work']=1801;
            }
            //特殊上班时间处理
            if (isset($special_leave_times[$special_day]) && intval(date('Hi',strtotime($special_leave_times[$special_day]['start_time']))) >= $start_work && $start_work >= 910) {
                $status[0]['name']='正常';
                $val['start_work']=900;
            }
            if (isset($status[0]) && isset($status[1]) && $status[0]['name'] == $status[1]['name'] && $status[0]['name'] == '正常'){
                unset($status[1]);
            }elseif(isset($status[0]) && isset($status[1]) && $status[0]['name'] == '正常'){
                unset($status[0]);
            }elseif(isset($status[0]) && isset($status[1]) && $status[1]['name'] == '正常'){
                unset($status[1]);
            }

            //请假原因去重
            if($val['reason']) {
                $val['reason'] = array_unique($val['reason']);
            }
            $val['status'] = $status;
            $result[$keep_date] = $val;
        }

        //下面是需要增加的，但现在uinfo还没有构造好  前面unset掉所有状态即可
        //1.迟到，2.早退,3.缺勤
        /*foreach ($result as $key => $value){
            if (!empty($result[$key]['status']) && $this->uinfo['user_type'] == 1 && $key >=$this->uinfo['user_type_date']) {
                if ($result[$key]['status'][0]['id'] == 1 || $result[$key]['status'][1]['id'] == 2 || $result[$key]['status'][0]['name'] == '上午缺勤' || $result[$key]['status'][1]['name'] == '下午缺勤') {
                    $result[$key]['status'][0]['id'] = 999;
                    $result[$key]['status'][0]['name'] = '正常';
                    unset($result[$key]['status'][1]);
                }
            }
        }*/

        //$resule 数组包括：1.该员工所有请假记录  2.当前一周的考勤状态
        return $result;
    }

    /**
     * 获取我的考勤及打卡信息
     *
     * @param mixed $user_id
     * @param int $start
     * @param int $end
     * @access public
     * @return void
     * @deprecated
     */
    public function get_my_attendance_new($user_id,$start = 0, $end = 0){
        $result = array();
        if(empty($user_id)){
            return $result;
        }
        apf_require_class('Bll_Attendance_LeaveRecord');
        $bll = new Bll_Attendance_LeaveRecord();
        $leave_record = $bll->get_leave_data($user_id);
        $result['allleaverecord'] = $leave_record;
        $tmp = $this->get_record_list($user_id,$start,$end);
        if(empty($tmp)) {
            return $result;
        }
        foreach ($tmp as $key => $val) {
            $keep_date = $val['access_date'];
            $access_date = $val['access_date'] = strtotime($val['access_date']);
            $val['month'] = date('m',$val['access_date']);
            $val['day'] = date('d',$val['access_date']);
            $val['week'] = $this->convert_chinese_week(date('w',$val['access_date']));
            $val['start'] = ($val['start_work'])?intval(substr($val['start_work'],0,2)).":".substr($val['start_work'],2,2):0;
            $val['end'] = ($val['end_work'])?substr($val['end_work'],0,2).":".substr($val['end_work'],2,2):0;
            $val['worktime'] = $this->get_work_time($val['start'],$val['end']);
            $val['workendtime'] = $this->get_workend_time($keep_date,$val['end']);
            if($keep_date == date('Ymd') && $val['end_work'] < 1800) {
                $val['end'] = 0;
            }
            $start_work = $val['start_work']= intval($val['start_work']);
            $end_work = $val['end_work'] = intval($val['end_work']);

            $status = array();
            //判断迟到状态
            $status_kq = $this->format_work_to_txt($start_work,$end_work);

            //检查用户是否有请假记录

            $have_holiday = false;
            if($start_work > 910 && $leave_record) {
                $ajust = mktime(9,31,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                //迟到
                $day_cd = date("Y-m-d",$ajust);

                foreach ($leave_record as $k => $v) {
                    $waitpass = false;
                    if($v['status'] == '已批准' || $v['status'] == '未审核') {
                        $waitpass = true;
                    }
                    if(($v['type'] !='缺勤' || $v['type'] != '迟到') && $ajust >= $v['start'] && $ajust <= $v['end'] && $waitpass) {
                        $status[0] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                        $reason = $v['leave_reason'];
                        //病假，陪产假特殊处理
                        if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                            if((time() - $v['apply_time_ext']) < 604800) {
                                $reason = $v['type']."<a href='/prof/?id=".$v['id']."'>上传证明</a>";
                            }
                        }
                        $val['reason'][] = $reason;
                        $have_holiday = true;
                    } elseif(($v['type'] == '缺勤' || $v['type'] =='迟到') && $day_cd == date("Y-m-d",$v['start']) && $waitpass){
                        $status[0] = array('id' => 999,'name' => $v['leave_reason'],'status'=>"({$v['status']})");
                        $val['reason'][] = $v['leave_reason'];
                    }
                 }
            }
            //检查下午是否有请假情况
            if($end_work < 1800 && $leave_record) {
                $afternoon = mktime(14,30,0,date('m',$access_date),date('d',$access_date),date('Y',$access_date));
                //迟到
                $day_cd = date("Y-m-d",$afternoon);
                foreach ($leave_record as $k => $v) {
                    $waitpass = false;
                    if($v['status'] == '已批准' || $v['status'] == '未审核') {
                        $waitpass = true;
                    }
                    if(($v['type'] !='缺勤' || $v['type'] != '迟到') && $afternoon >= $v['start'] && $afternoon <= $v['end'] && $waitpass) {
                        if(!$have_holiday) {
                        $status[0] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                        $reason = $v['leave_reason'];
                        //病假，陪产假特殊处理
                        if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                            if((time() - $v['apply_time_ext']) < 604800) {
                                $reason = $v['type']."<a href='/proof/?id={$v['id']}'>上传证明</a>";
                            }
                        }
                        $val['reason'][] = $reason;
                        }
                        unset($status[1]);
                    } elseif(($v['type'] == '缺勤' || $v['type'] =='迟到') && $day_cd == date("Y-m-d",$v['start']) && $waitpass){
                        $status[0] = array('id' => 999,'name' => $v['leave_reason'],'status'=>"({$v['status']})");
                        $val['reason'][] = $v['leave_reason'];
                        unset($status[1]);
                    }
                }
            }
            //请假原因去重
            if($val['reason']) {
                $val['reason'] = array_unique($val['reason']);
            }
            $val['status'] = $status;
            $val['status_kq'] = $status_kq;
            $result[$keep_date] = $val;
        }
        return $result;
    }

    /**
     * 检测当前的打卡记录
     * @param int $day 20120901
     * @param int $start_work 第一次打卡时间 0910
     * @param int $end_work 最后一次打卡时间 1805
     * @param int $deconfig 读取那份配置
     */
    public function check_day_work_status($day,$start_work,$end_work,$deconfig=0){
        $config = $deconfig.'_default';
        $checklist = APF::get_instance()->get_config($config);
        $status = array(
                0=>$checklist['am_q'],
                1=>$checklist['pm_q']
        );
        if(empty($start_work) && empty($end_work)){
            return $status;
        }
        if($start_work >=1800 && $end_work >=1800){
            return $status;
        }
        apf_require_class("Bll_Attendance_SpecialDay");
        $spday_bll = new Bll_Attendance_SpecialDay();
        $speciday = $spday_bll->get_special_day($day);

        //特殊工作日,上下行时间不是9-18
        if(!empty($speciday) && $speciday['status']==1){
            if($speciday['start_time']==900){
                foreach($checklist['am'] as $val){//上午考勤状态
                    if($start_work < $val['end'] && $start_work > $val['start']){
                        $status[0]=$val['status'];
                        break;
                    }
                }
            }else{
                if($start_work <= $speciday['start_time']){
                    $status[0]=5;
                }
            }
            if($end_work >= $speciday['end_time'] && $start_work <$speciday['end_time']){
                $status[1]=6;
            }
            return $status;
        }
        foreach($checklist['am'] as $val){//上午考勤状态
            if($start_work < $val['end'] && $start_work > $val['start']){
                $status[0]=$val['status'];
                break;
            }
        }
        if($start_work > 1800){
            return $status;
        }
        foreach($checklist['pm'] as $val){//下午考勤状态
            if($end_work > $val['start'] && $end_work < $val['end']){
                    $status[1]=$val['status'];
                    break;
            }
            if($status[1]==2 && $start_work>1430){
                $status[1]=$checklist['pm_q'];
            }
        }
        return $status;
    }

    //格式化状态为文字
    /**
     *
     * @param unknown_type $start_work
     * @param unknown_type $end_work
     * @return string|multitype:string unknown
     * @deprecated
     */
    public function format_work_to_txt($start_work,$end_work){
        $txt = '缺勤';
        $status = $this->check_day_work_status($start_work,$end_work,$deconfig=0);
        if(empty($status)){
            return $txt;
        }
        $checklist = APF::get_instance()->get_config('kq_status');
        $status_ext = APF::get_instance()->get_config('kq_status_ext');
        $key = $status[0].'0'.$status[1];
        $txt = $checklist[$key];
        $array = array('code'=>$key,'des'=>$checklist[$key],'id'=>$status_ext[$key]);
        return $array;
    }

    /**
     * 计算加班工时,默认从19:00后打卡，才算加班，加班时间从19:00到24:00
     * @param unknown_type $end
     */
    private function get_workend_time($day,$end = 0) {
        $d = 19;
        $result = '0';
        if(empty($end) || empty($day)) {
            return $result;
        }

        $nowday = $day.' '.$end;
        $time = strtotime($nowday);
        $h = date('H',$time);
        if($h>=$d) {
            $time = strtotime($nowday)-strtotime($day.' 18:00');
            $result = floor(($time/3600)*10)/10;
        }
        $result = $result <0 ? 0 : $result;
        return $result;
    }
    /**
     * 计算工时
     */
    private function get_work_time($start = 0,$end = 0) {
        $result = '0';
        if(empty($start) || empty($end)) {
            return $result;
        }

        if ($start < 1200) {
            if ($end > 1300) {
                if ($end > 1800) {
                    $worktime = strtotime(1800)-strtotime($start) -3600;
                }else{
                    $worktime = strtotime($end)-strtotime($start) -3600;
                }
            }else{
                $worktime = strtotime($end)-strtotime($start);
            }

        }else
        {
            if ($end > 1800) {
                $worktime = strtotime(1800)-strtotime($start);
            }else
            {
                $worktime = strtotime($end)-strtotime($start);
            }
        }

        $worktime = floor(($worktime/3600)*10)/10;
        return $worktime;
    }

    public function convert_chinese_week($week){
        $wk = array(0 => '日',1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六');
        return $wk[$week];
    }
    public function get_user_day($uid,$day){
        $where = array(
                'user_id'=>$uid,
                'access_date'=>$day
                );
        $data = $this->get_app_dao()->find($where);
        return $data;
    }

    /**
     * 数据写入到打卡记录表中  临时提供给移动端使用
     *
     * @param mixed $data
     * @access private
     * @return void
     */
    public function insert_data_to_all_record($data) {
        $result = '';
        if(empty($data) || empty($data['code'])) {
            return $result;
        }
        $q = "SELECT id FROM access_all_record WHERE code = '{$data['code']}' AND access_date = '{$data['access_date']}' AND access_time = '{$data['access_time']}'";
        $b = $this->get_all_dao()->execute($q,array(),'attendance_master');
        if(isset($b['id'])) {
            return $result;
        }
        $sql = "INSERT INTO access_all_record(code,access_date,access_time,machine_num,updated) VALUE(?,?,?,?,?)";
        $result = $this->get_all_dao()->execute($sql, array($data['code'],$data['access_date'],$data['access_time'],$data['machine_num'],$data['updated']), 'attendance_master');
        return $result;
    }

    /**
     * 获取所有当天所有打卡记录
     *
     * @param mixed $data
     * @access private
     * @return void
     */
    public function get_all_record($data) {
        $result = '';
        if(empty($data) || empty($data['code'])) {
            return $result;
        }
        $q = "SELECT * FROM access_all_record WHERE code = '{$data['code']}' AND access_date = '{$data['access_date']}'";
        $b = $this->get_all_dao()->execute($q,array(),'attendance_master');
        if($b) {
            $result = $b;
        }
        return $result;
    }

     /**
     *  临时提供给移动端使用
     *
     * **/
    public function insert_data_to_day_record($data,$user_id) {
        $result = '';
        if(empty($data)) {
            return $result;
        }
        if(empty($user_id)) {
            return $result;
        }
        $nowdate = $data['access_date'];
        //检查当天是否已有打开记录
        $q = "SELECT * FROM access_day_record WHERE user_id = $user_id AND access_date = '{$nowdate}'";
        $tmp = $this->get_app_dao()->execute($q,array(),'attendance_master');
        //如无当天数据新插入数据
        if(!isset($tmp['id'])) {
            $i = "INSERT INTO access_day_record(access_date,start_work,user_id,created) VALUE(?,?,?,?)";
            $result = $this->get_app_dao()->execute($i, array($nowdate,$data['access_time'],$user_id,time()),'attendance_master');
        } else {
            //下次打卡需大于上次
            if($data['access_time'] > $tmp['end_work']) {
                $id = $tmp['id'];
                $now = time();
                $u = "UPDATE access_day_record SET end_work = '{$data['access_time']}',updated = $now WHERE id = $id";
                $result =  $this->get_app_dao()->execute($u, array(),'attendance_master');
            } elseif($data['access_time'] < $tmp['start_work']) {
                $id = $tmp['id'];
                $now = time();
                $u = "UPDATE access_day_record SET start_work = '{$data['access_time']}',updated = $now WHERE id = $id";
                $result =  $this->get_app_dao()->execute($u, array(),'attendance_master');
            } else {

           }
        }
        return $result;
    }

    public function get_access_by_uids($uids=array()){
        if(empty($uids)){
           return array();
        }
        //get user code
        $codewhere = array(
                "code IN('".implode("','", $uids)."') AND 1=?"=>1
                );
        apf_require_class('Bll_Attendance_UserInfo');
        $user_bll = new Bll_Attendance_UserInfo();

        $code_data = $user_bll->get_user_by_condition($codewhere);
        $uids = array();
        if(!empty($code_data)){
            foreach($code_data as $val){
                $uids[$val['user_id']] = $val['code'];
            }
        }
        $uidst= array_keys($uids);
        $where=array(
                'access_date'=>date('Ymd'),
                "user_id IN(".implode(',', $uidst).") AND 1=?"=>1
                );
        $data = $this->get_app_dao()->find($where);
        $result = array();
        foreach($uids as $val){
            $result[$val] = 0;
        }
        if(!empty($data)){
            foreach($data as $val){
                if($val['start_work']){
                    $code = $uids[$val['user_id']];
                    $result[$code]=1;
                }
            }
        }
        return $result;
    }


    /**
     * @return Dao_Attendance_AccessDayRecord
     */
    private function get_app_dao(){
        $dao = $this->get_dao('Dao_Attendance_AccessDayRecord');
        return $dao;
    }

    /**
     * @return Dao_Attendance_AccessAllRecord
     */
    private function get_all_dao(){
        $dao = $this->get_dao('Dao_Attendance_AccessAllRecord');
        return $dao;
    }
}
